<?php

require_once(WSB_XTENSION_PATH . 'title/models/DbTable/Table.php');

class Title_IndexController extends WSB_Xtension_Content
{

	protected $_noViewRenderer = true;

	public function indexAction()
	{
		$table = $this->getTable();
		$where = $table->getAdapter()->quoteInto('id=?', $this->_container->item_id);
		$row = $table->fetchRow($where);
		if($row instanceof WSB_Db_Row) {
			$html = sprintf($this->_getHtmlTag($row), $row->title);
			if(!empty($row->subtitle)) {
				$html .= sprintf('<p class="xt-title-subtitle">%s</p>', $row->subtitle);
			}
			$this->addContent($this->wrapInBaseClass($html));
		}
	}

	public function getTable()
	{
		return new Title_Model_DbTable_Table();
	}

	private function _getHtmlTag(WSB_Db_Row $row)
	{
		$type = intval($row->type);
		$type = min($type, 6);
		$type = max($type, 1);
		// Dies ergibt folgendes:
		// $type >= 1 und $type <= 6
		return sprintf('<h%1$d>%%s</h%1$d>', $type);
	}

}
